package com.mywiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mywiki.domain.Ebook;
import com.mywiki.domain.EbookExample;
import com.mywiki.mapper.EbookMapper;
import com.mywiki.req.EbookQueryReq;
import com.mywiki.req.EbookSaveReq;
import com.mywiki.resp.EbookQueryResp;
import com.mywiki.resp.PageResp;
import com.mywiki.utils.CopyUtil;
import com.mywiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * describe:
 *
 * @author: robin
 * @date 2024/1/19 11:22
 */
@Service
public class EbookService {

    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<EbookQueryResp> list(EbookQueryReq req){
        String name = null;
        if(StringUtils.isEmpty(req.getName())){
            name = "";
        }
        else{
            name = req.getName();
        }

        PageHelper.startPage(req.getPage(), req.getSize());

        EbookExample ebookExample = new EbookExample();
        if(!ObjectUtils.isEmpty(name)){
            ebookExample.createCriteria().andNameLike("%" + name + "%");
        }
        if(!ObjectUtils.isEmpty(req.getCategoryId2())){
            ebookExample.createCriteria().andCategory2IdEqualTo(req.getCategoryId2());
        }

        ebookExample.setOrderByClause("id desc");
        List<Ebook> ebooks = ebookMapper.selectByExample(ebookExample);

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebooks);
        LOG.info("总行数: {}", pageInfo.getTotal());
        LOG.info("总页数: {}", pageInfo.getPages());

        //        List<EbookQueryResp> ebookQueryResps = new ArrayList<>();
        //        for(Ebook ebook : ebooks){
        //            EbookQueryResp resp = new EbookQueryResp();
        //            BeanUtils.copyProperties(ebook, resp);
        //            ebookQueryResps.add(resp);
        //        }
        List<EbookQueryResp> ebookQueryResps = CopyUtil.copyList(ebooks, EbookQueryResp.class);

        PageResp<EbookQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(ebookQueryResps);

        return pageResp;
    }

    /**
     * 保存ebook数据
     */
    public void save(EbookSaveReq req){
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        if(ObjectUtils.isEmpty(req.getId())){
            // 新增
            ebook.setId(snowFlake.nextId() );
            ebookMapper.insert(ebook);
        }
        else{
             ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    public void delete(long id){
        ebookMapper.deleteByPrimaryKey(id);
    }
}
